import AbstractField from '@/components/hhd/design/FormDesigner/field/comps/AbstractField';
import {createDefaultFieldElement} from '@/utils/FormDesignUtils';
import {createField} from '@/components/hhd/design/FormDesigner/types';
import {guid, isEmpty} from '@/utils';
import $ from 'jquery';
import {message} from "ant-design-vue";

export default class Number extends AbstractField {
  getFieldComponentTitle() {
    return '数字';
  }

  getFieldComponentName() {
    return 'number';
  }

  getDefaultConfig() {
    return new Promise((resolve) => {
      let field = createField();
      field.id = guid();
      field.component = 'number';
      field.fieldName = '数字';
      field.label = '数字';
      field.placeholder = '请输入数字';
      field.length = 2;
      resolve(field);
    });
  }

  checkField(field) {
    if (super.checkField(field)) {
      if (!isEmpty(field.min_value) && !isEmpty(field.max_value) && field.max_value < field.min_value) {
        message.error(field.label + '最大值必须大于等于最小值');
        return false;
      } else
        return true;
    } else {
      return false;
    }
  }

  createFieldElement() {
    return new Promise((resolve) => {
      resolve(this.createDefaultFieldElement());
    });
  }

  setFieldTitle(el, fieldConfig) {
    $(el).find('span[data-role=field-title]').html(fieldConfig.label);
  }

  createDefaultFieldElement() {
    let res = createDefaultFieldElement();
    res.innerHTML = '<span data-role="field-title">数字</span>';
    return res;
  }
};
